package PenguinsFarm.Penguins.Stage
{
	import PenguinsFarm.Penguins.PenguinsRenderer;
	
	import flash.display.MovieClip;
	import flash.geom.Vector3D;

	public class EatStage implements IPenguinsStage
	{
		private var target:PenguinsRenderer;
		private var timeFactor:Number		=	0;
		private var velocityFactor:Number	=	0;
		
		private var fromVec:Vector3D;
		private var toVec:Vector3D;		
		private var difVec:Vector3D;
		
		public function EatStage( target:PenguinsRenderer, foodPosition:Vector3D )
		{
			this.target	=	target;
			
			var mc:MovieClip	=	target.getAssetPtr();
			if( mc )
				mc.gotoAndPlay( "moving" );
			
			fromVec			=	new Vector3D( target.x, target.y );
			toVec			=	new Vector3D( foodPosition.x, foodPosition.y );	
			difVec			=	toVec.subtract( fromVec );
			velocityFactor	=	120.0 / difVec.length;
		}
		
		public function setTargetPosition( targetPosition:Vector3D ):void
		{	
			toVec			=	new Vector3D( targetPosition.x, targetPosition.y );	
			difVec			=	toVec.subtract( fromVec );
			velocityFactor	=	45.0 / difVec.length;
		}
		
		public function onFrameUpdate( deltaTime:Number):void
		{
			timeFactor	+=	deltaTime * velocityFactor;
			
			var scaleVec:Vector3D	=	difVec.clone();
			scaleVec.scaleBy( timeFactor );
			
			var curVec:Vector3D	=	fromVec.add( scaleVec );
			target.x	=	curVec.x;
			target.y	=	curVec.y;
			
			// collision with lake
			/*var penguinsContainer:DisplayObject	=	Global.penguinsCageScene.cageBackground.penguinsContainer;
			if( Global.penguinsCageScene.lake.rendererObj.hitTestPoint( target.x + penguinsContainer.x, target.y + penguinsContainer.y, true ) )
			{
			var jumpStage:IPenguinsStage	=	new JumpStage( target, 30, toVec.x - target.x );
			target.PenguinsStage			=	jumpStage;
			}*/
		}
		
		public function dispose():void
		{
			fromVec	=	null;
			toVec	=	null;
			difVec	=	null;
		}		
		
		public function isCompleteStage():Boolean
		{
			return false;
		}
	}
}